package fr.coupedumonde2014.app.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.HashMap;

import fr.coupedumonde2014.app.R;
import fr.coupedumonde2014.app.fragment.ListMatchFragment;
import fr.coupedumonde2014.app.fragment.RankFragment;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    private String[] TITLES = {"A VENIR", "MES PARIS", "CLASSEMENT"};

    private HashMap<Integer, Fragment> fragments = new HashMap<Integer, Fragment>();

    public TabsPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        TITLES = new String[]{
            context.getString(R.string.a_venir).toUpperCase(),
            context.getString(R.string.mes_paris).toUpperCase(),
            context.getString(R.string.classement).toUpperCase(),
        };
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public Fragment getItem(int index) {

        if (fragments.containsKey(index))
            return fragments.get(index);
        else {

            Fragment f = null;
            switch (index) {
                case 0:
                    f = new ListMatchFragment(true);
                    break;
                case 1:
                    f = new ListMatchFragment(false);
                    break;
                case 2:
                   return new RankFragment();
                default:
                    f = new Fragment();
                    break;
            }

            fragments.put(index, f);

            return f;

        }
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return TITLES.length;
    }

    public void refresh() {
        for(Fragment f : fragments.values()){
            if(f instanceof ListMatchFragment)
                ((ListMatchFragment)f).refresh();
        }
    }
}